package com.mingtu.guards.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ScreenUtils;
import com.blankj.utilcode.util.SizeUtils;
import com.blankj.utilcode.util.StringUtils;
import com.bumptech.glide.Glide;
import com.google.android.material.imageview.ShapeableImageView;
import com.luck.picture.lib.config.PictureMimeType;
import com.mingtu.common.base.BaseApplication;
import com.mingtu.guards.R;
import com.mingtu.guards.bean.InfraredRecordImgBean;

import java.util.ArrayList;
import java.util.List;

import static com.mingtu.guards.utils.MyConstant.ItemDecorationSpaces;


public class InfraredPictureAdapter extends RecyclerView.Adapter<InfraredPictureAdapter.ActivityHolder> implements View.OnClickListener {
    private Context context;
    private OnItemClickListener mOnItemClickListener = null;
    private List<InfraredRecordImgBean.DataBean.ListBean> mData = new ArrayList<>();

    public InfraredPictureAdapter(Context context, List<InfraredRecordImgBean.DataBean.ListBean> mData) {
        this.context = context;
        this.mData = mData;
    }

    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemViewType(int position) {
        if (mData != null) {
            if (mData.size() > 0) {
                InfraredRecordImgBean.DataBean.ListBean listBean = mData.get(position);
                String similar = listBean.getSimilar();
                if (!StringUtils.isEmpty(similar)) {
                    return 1;
                }
            }
        }

        return 2;
    }

    @Override
    public ActivityHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        // 实例化展示的view

        int layout = R.layout.item_picture_infrared;
        if (viewType == 1) {
            layout = R.layout.item_picture_infrared;
        } else if (viewType == 2) {
            layout = R.layout.item_picture_infrared2;
        }

        View view = LayoutInflater.from(parent.getContext()).inflate(layout, parent, false);
        // 实例化viewholder
        ActivityHolder viewHolder = new ActivityHolder(view);
        viewHolder.itemView.setOnClickListener(this);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ActivityHolder holder, int position) {

        if (mData != null) {
            if (mData.size() > 0) {
                InfraredRecordImgBean.DataBean.ListBean listBean = mData.get(position);
                String url = listBean.getUrl(); //可能是图片 可能是视频
                String status = listBean.getStatus(); //0:未认证  1：已认证
                String cameraName = listBean.getCameraName();
                String createTime = listBean.getCreateTime();
                String similar = listBean.getSimilar();
                String photoDate = listBean.getDate();
                String photoTime = listBean.getTime();

                if (!StringUtils.isEmpty(url)) {

                    boolean isHasVidep = PictureMimeType.isUrlHasVideo(url);
                    if (isHasVidep) {
                        holder.ivVideo.setVisibility(View.VISIBLE);
                    } else {
                        holder.ivVideo.setVisibility(View.GONE);
                    }

                    Glide.with(context)
                            .asDrawable()
                            .load(url)
                            .thumbnail(0.5f)
                            .apply(BaseApplication.getRequestOptions())
                            .into(holder.ivPicture);

                }

                if (!StringUtils.isEmpty(similar)) {
                    holder.tvName.setVisibility(View.VISIBLE);
                    holder.tvName.setText(similar);
                } else {
                    holder.tvName.setVisibility(View.GONE);
                }

                if (!StringUtils.isEmpty(cameraName)) {
                    holder.tvDequName.setText("设备：" + cameraName);
                } else {
                    holder.tvDequName.setText("设备：--");
                }

                if (!StringUtils.isEmpty(createTime)) {
                    holder.tvTime.setText(createTime);
                } else {
                    holder.tvDequName.setText("时间：--/--/--");
                }
            }
        }
        //将position保存在itemView的Tag中，以便点击时进行获取
        holder.itemView.setTag(position);
    }

    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            //注意这里使用tag方法获取position
            try {
                mOnItemClickListener.onItemClick(v, (int) v.getTag());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public void upData(List<InfraredRecordImgBean.DataBean.ListBean> mData) {
        this.mData = mData;
        notifyDataSetChanged();
    }


    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    public static interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    public static class ActivityHolder extends RecyclerView.ViewHolder {
        private final View convertView;
        public ShapeableImageView ivPicture;
        public ImageView ivVideo;
        public LinearLayout layout;
        public TextView tvName;
        public TextView tvTime;
        public TextView tvDequName;

        public ActivityHolder(View itemView) {
            super(itemView);
            convertView = itemView;
            layout = (LinearLayout) convertView.findViewById(R.id.layout);
            ivPicture = (ShapeableImageView) convertView.findViewById(R.id.iv_picture);
            ivVideo = (ImageView) convertView.findViewById(R.id.iv_video);
            tvDequName = (TextView) convertView.findViewById(R.id.tv_dequ_name);
            tvName = (TextView) convertView.findViewById(R.id.tv_name);
            tvTime = (TextView) convertView.findViewById(R.id.tv_time);


            ViewGroup.LayoutParams lp = layout.getLayoutParams();
            int screenWidth = ScreenUtils.getScreenWidth(); //照片一行4个，间隙是5dp
            int mWidth = (screenWidth - SizeUtils.dp2px(ItemDecorationSpaces) * 3) / 2;
//            int mHeight = mWidth * SizeUtils.dp2px(218) / SizeUtils.dp2px(165);
            lp.width = mWidth;
            lp.height = ViewGroup.LayoutParams.WRAP_CONTENT;
            layout.setLayoutParams(lp);

        }
    }

}
